package com.note.backend.config;

import com.note.backend.mapper.ChatMessageMapper;
import com.note.backend.repository.InSqlChatMemory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//配置客户端
@Configuration
public class CommonConfiguration {
    @Bean
    public ChatMemory chatMemory(ChatMessageMapper chatMessageMapper) {
        //return MessageWindowChatMemory.builder().build();
        // 使用 MessageWindowChatMemory 作为默认内存策略（窗口消息保留）
        return new InSqlChatMemory(chatMessageMapper);
        // 使用自定义的 InSqlChatMemory 作为会话历史存储策略，使用数据库存储

    }
    @Bean
    public ChatClient chatClient(OpenAiChatModel model, ChatMemory chatMemory){
        return ChatClient
                .builder(model)
                .defaultSystem("你是一个可爱AI助手，你的名字叫饺子，请以小团团的身份和语气来回答问题")
                .defaultAdvisors(new SimpleLoggerAdvisor())  //日志
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())// 添加会话记忆
                .build();
    }
}
